<?php

namespace App\Http\Controllers;

use App\Models\Article;
use App\Models\ArticleCategory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;

class ArticleController extends Controller
{
    //分类
    public function cate()
    {
        $cate = new ArticleCategory();
        $data = $cate->arrayAllCate();

        if(!empty($data)){
            $data[0]['article'] = ArticleCategory::find($data[0]['id'])->article()
                ->select('id','cate_id','title','description','views','created_at')
                ->get();
        }

        return response()->json([
            'code'=> 1,
            'msg' => '',
            'data' => $data
        ]);
    }

    //分类-文章列表
    public function articleList(Request $request)
    {
        $cate_id = $request->input('cate_id');
        if($cate_id){
            $data = ArticleCategory::find($cate_id)->article()
                ->select('id','cate_id','title','description','views','created_at')
                ->get();

            return response()->json([
                'code' =>1,
                'msg' =>'',
                'data' => $data
            ]);
        }else{
            return response()->json([
                'code' =>0,
                'msg' =>'没有分类id',
                'data' => ''
            ]);
        }
    }

    //文章详情
    public function articleDetail(Request $request)
    {
        $article_id = $request->input('article_id');
        if($article_id){
            //增加文章流浪量
            Article::where('id',$article_id)->increment('views',1);

            $data = Article::find($article_id)->first();

            return response()->json([
                'code' =>1,
                'msg' =>'',
                'data' => $data
            ]);
        }else{
            return response()->json([
                'code' =>0,
                'msg' =>'没有文章id',
                'data' => ''
            ]);
        }
    }


}
